package com.epoint.mobileoa.personalized.epointcompany.ui.carnumberquery;

import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.tools.XMLUtil;
import com.epoint.mobileoa.ui.contacts.ContactsDetailView;

/**
 * @author liyc
 * @time 2013-2-17 下午3:07:12
 * @annotation 车牌号查询
 */
public class CarNumberQueryView extends SuperView {

	private ListView lv;
	private EditText etCondition;
	private ImageView ivQuery;
	private List<CarNumberModel> nlist;

	@Override
	public void initControls() {
		lv = (ListView) findViewById(R.id.lv);
		lv.setOnItemClickListener(this);
		etCondition = (EditText) findViewById(R.id.etCondition);
		ivQuery = (ImageView) findViewById(R.id.ivQuery);
		ivQuery.setOnClickListener(this);
		
	}
	
	@Override
	public void onClick(View v) {
		if(v==ivQuery&&etCondition.getText().toString().trim().length()>0)
		{
			//ToastHelp.showAndroidToastWorning(this, "查询中...");
			HashMap<String, Object> params = new HashMap<String, Object>();
			params.put("condition", etCondition.getText().toString());
			new CarNumberQueryTask(this, 1, params, "查询中...", 2);
			InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(etCondition.getWindowToken(), 0);
		}
	}

	@Override
	public void refresh(Object... param) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void refreshForNewTask(int TaskId, Object... param) {
		// TODO Auto-generated method stub
		if(TaskId==1)
		{
			String bs = param[0].toString();
			//Toast.makeText(this, bs, Toast.LENGTH_LONG).show();//(this, bs);
			nlist = XMLUtil.DomAnalysisCommon(bs, CarNumberModel.class);
			if(nlist.size()==0)
			{
				Toast.makeText(this, "无查询结果", Toast.LENGTH_SHORT).show();
				return;
			}
			LstAdapter adapter = new LstAdapter();
			lv.setAdapter(adapter);
		}
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		Intent intent = new Intent(this,
				ContactsDetailView.class);
		intent.putExtra("userguid", nlist.get(position).userguid);
		startActivity(intent);
	}

	@Override
	public int getLayoutInt() {
		// TODO Auto-generated method stub
		return R.layout.carnumberqueryview;
	}

	class LstAdapter extends BaseAdapter
	{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return nlist.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				LayoutInflater mInflater = LayoutInflater.from(getContext());
				convertView = mInflater.inflate(R.layout.carquerylstlayout, null);
				holder.tvInfo = (TextView) convertView
						.findViewById(R.id.tvInfo);
				holder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			CarNumberModel c = nlist.get(position);
			holder.tvInfo.setText(c.displayname+"("+c.CarNumber+")");
			setTextViewFontColor(holder.tvInfo);
			return convertView;
		}

	}
	
	class ViewHolder
	{
		TextView tvInfo;
		ImageView ivIcon;
	}
}
